package com.sanhe.rfmuscle.http;

import static com.menu.app.http.req.Req.JSON;

import com.google.gson.Gson;
import com.menu.app.bean.DayRecord;
import com.menu.app.db.bean.CusUsage;
import com.menu.app.http.req.HttpReq;
import com.menu.app.http.req.Req;
import com.menu.app.it.ReqCallback;
import com.menu.app.util.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;

import okhttp3.RequestBody;

public class PdfReq implements HttpReq<PdfBean> {

    String docId, customId;
    List<DayRecord> list;

    public PdfReq(String docId, String customId, List<DayRecord> list) {
        this.docId = docId;
        this.customId = customId;
        this.list = list;
    }

    private RequestBody getRequestBody() {
        try {
            JSONObject obj = new JSONObject();
            obj.put("beauticianId", docId);
            obj.put("customId", customId);

            JSONArray array = new JSONArray();

            Gson g = new Gson();
            for (DayRecord item : list) {
                if (item.select && item.record != null) {
                    JSONArray opt = new JSONArray();
                    JSONObject json = new JSONObject();
                    for (CusUsage data : item.record) {
                        HashMap map = new HashMap();
                        map.put("mainFrequency", data.info.get("mf") + "");
                        map.put("mainTime", data.info.get("mt") + "");
                        map.put("masterFluence", data.info.get("fluence1") + "");
                        map.put("masterRf", data.info.get("rf1") + "");
                        map.put("masterTime", data.info.get("time1") + "");
                        map.put("mode", data.info.get("mode") + "");
                        map.put("slaveFluence", data.info.get("fluence2") + "");
                        map.put("slaveRf", data.info.get("rf2") + "");
                        map.put("slaveTime", data.info.get("time2") + "");
                        map.put("subFrequency", data.info.get("sf") + "");
                        map.put("subTime", data.info.get("st") + "");
                        map.put("treatTime", data.point + "");
                        String s = g.toJson(map);
                        JSONObject tmp = new JSONObject(s);
                        opt.put(tmp);
                    }
                    json.put("deviceDate", item.getDay(1));
                    json.put("historyPdfList", opt);
                    array.put(json);
                }
            }
            obj.put("deviceNum", Utils.getMsn());
            obj.put("deviceDataPdfList", array);
            return RequestBody.create(JSON, obj.toString());
        } catch (Throwable e) {
            e.printStackTrace();
            return RequestBody.create(JSON, "{}");
        }
    }

    @Override
    public void req(ReqCallback callback) {
        Req.post("system/app/device/ms006/pdf", getParam(), getHeader(), getRequestBody(), new PdfParser(), callback);
    }

    public PdfBean req() {
        try {
            return Req.post("system/app/device/ms006/pdf", getParam(), getHeader(), getRequestBody(), new PdfParser(), 0);
        } catch (Throwable e) {
            return null;
        }
    }
}
